package com.syj.collect.Present;

import android.util.Log;

import com.google.gson.Gson;
import com.syj.collect.base.BaseBean;
import com.syj.collect.base.BasePresent;
import com.syj.collect.entry.AllFragmentBean;
import com.syj.collect.entry.TaskBean;
import com.syj.collect.entry.UserInfoBean;
import com.syj.collect.http.BaseObserver;
import com.syj.collect.http.BaseRequest;
import com.syj.collect.utils.GsonUtil;
import com.syj.collect.utils.SPUtil;
import com.syj.collect.view.AllFragmentView;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class AllFragmentPresent extends BasePresent<AllFragmentView> {

    public AllFragmentBean.DataBean.ItemBean commBean = null;//更新

    public void getCollectList(int status) {
        String value = SPUtil.getPrefString("userInfo", "");
        Gson gson = new Gson();
        UserInfoBean.UserInfo gBean = gson.fromJson(value, UserInfoBean.UserInfo.class);
        HashMap hashMap = new HashMap();
        hashMap.put("status", "" + status);//1 未采集  2采集中  3已采集
        hashMap.put("userId", gBean.getUserId());
        hashMap.put("grade", "1");//1常规  2紧急
        //hashMap.put("endTime", "");
        //hashMap.put("startTime", "");


//        String md5string=StringUtils.initString(hashMap);
//        hashMap.put("sign",md5string);

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), gson.toJson(hashMap));
        BaseRequest.getInstance().getApiServise().getUserTask1(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public boolean onSuccess(String value) {
                        Gson gson = new Gson();
                        TaskBean gBean = gson.fromJson(value, TaskBean.class);

//                        Log.e("task结果?", value);
                        if (gBean.getCode() == 0) {
                            view.initSuccess(status, null);
                        } else {
                            view.initFail(gBean.getMsg());
                        }

                        return false;
                    }

                    @Override
                    public void onFailure(Throwable e) throws Exception {
                        view.initFail("网络异常");
                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }

    public void getTaskList(int status,int page) {
        HashMap hashMap = new HashMap();
        //hashMap.put("isCollect", "1");//可选
        String value = SPUtil.getPrefString("userInfo", "");
        Gson gson = new Gson();
        UserInfoBean.UserInfo gBean = gson.fromJson(value, UserInfoBean.UserInfo.class);
        hashMap.put("userId", gBean.getUserId());//不写为所有用户
        hashMap.put("auditStatus", "" + status);//审核状态  0 这里不用未审核 2 审核通过 3审核未通过
        hashMap.put("page", page + "");
        String str = gson.toJson(hashMap);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), str);
        // Log.e("1", str);

        BaseRequest.getInstance().getApiServise().getUserCollectList(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public boolean onSuccess(String value) {
//                        Log.e("task1结果?", value);
                        Gson gson = new Gson();
                        AllFragmentBean gBean = gson.fromJson(value, AllFragmentBean.class);
                        if (gBean.getCode() == 0) {
                            if (gBean.getData().getTotalCount() >= 0) {
                                view.initTaskSuccess(status,gBean.getData().getCurrPage(), gBean.getData().getList());
                            }else {
                                view.initFail(gBean.getMsg());
                            }
                        } else {
                            view.initFail(gBean.getMsg());
                        }


                        return false;
                    }

                    @Override
                    public void onFailure(Throwable e) throws Exception {
                        //view.initFail("网络异常");
                    }

                    @Override
                    public void onEnd() {

                    }
                });

    }

    //已提交

    public void getIsubmit(int page) {
        HashMap hashMap = new HashMap();
        //hashMap.put("isCollect", "1");//可选
        String value = SPUtil.getPrefString("userInfo", "");
        Gson gson = new Gson();
        UserInfoBean.UserInfo gBean = gson.fromJson(value, UserInfoBean.UserInfo.class);
        hashMap.put("userId", gBean.getUserId());//不写为所有用户
        hashMap.put("isSubmit", "1");
        hashMap.put("page", page + "");
        String str = gson.toJson(hashMap);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), str);
        // Log.e("1", str);

        BaseRequest.getInstance().getApiServise().getUserCollectList(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public boolean onSuccess(String value) {
//                        Log.e("task1结果?", value);
                        Gson gson = new Gson();
                        AllFragmentBean gBean = gson.fromJson(value, AllFragmentBean.class);
                        if (gBean.getCode() == 0) {
                            if (gBean.getData().getTotalCount() >=0) {
                                view.initTaskSuccess(500, gBean.getData().getCurrPage(),gBean.getData().getList());
                            }
                        } else {
                            view.initFail(gBean.getMsg());
                        }
                        return false;
                    }

                    @Override
                    public void onFailure(Throwable e) throws Exception {
                        view.initFail("网络异常");
                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }

    //全部数据

    public void getAll(int page) {
        HashMap hashMap = new HashMap();
        //hashMap.put("isCollect", "1");//可选
        String value = SPUtil.getPrefString("userInfo", "");
        Gson gson = new Gson();
        UserInfoBean.UserInfo gBean = gson.fromJson(value, UserInfoBean.UserInfo.class);
        hashMap.put("userId", gBean.getUserId());//不写为所有用户
        hashMap.put("isSubmit", "1");
        hashMap.put("page", page + "");

        String str = gson.toJson(hashMap);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), str);
        // Log.e("1", str);

        BaseRequest.getInstance().getApiServise().getUserCollectList(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public boolean onSuccess(String value) {
//                        Log.e("task1结果all?", value);
                        AllFragmentBean gBean1 = GsonUtil.gsonToBean(value, AllFragmentBean.class);
                        // List<AllFragmentBean.DataBean.ItemBean> taskListBeanData = userTaskListBean.getData().getList();
                        if (gBean1.getCode() == 0) {
                            if (gBean1.getData().getTotalCount() >= 0) {
                                view.initTaskSuccess(200, gBean1.getData().getCurrPage(), gBean1.getData().getList());
                            }


                        } else {
                            view.initFail(gBean1.getMsg());
                        }
                        return false;
                    }

                    @Override
                    public void onFailure(Throwable e) throws Exception {
                        view.initFail("网络异常");
                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }


    public void addCollect(int status, AllFragmentBean.DataBean.ItemBean bean, boolean isCollect) {
        BaseRequest.getInstance().getApiServise().addCollect(bean.getId(), isCollect ? 1 : 0)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    public boolean onSuccess(String value) {
//                        Log.e("tag结果 :", value);
                        Gson gson = new Gson();
                        BaseBean gBean = gson.fromJson(value, BaseBean.class);
                        if (gBean.getCode() == 0) {
                            view.initSuccess(status, gBean.getMsg());
                        } else {
                            view.initFail(gBean.getMsg());
                        }


                        return false;
                    }

                    @Override
                    public void onFailure(Throwable e) throws Exception {
                        view.initFail("网络异常");
                    }

                    @Override
                    public void onEnd() {

                    }
                });

    }


}
